﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using PowerCreator.LMS.Web.Project;
using PowerCreator.LMS.Project.Entity;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Web.Catalog;

public partial class Theme_index_StudyProject : PowerCreator.LMS.Web.Base.BasePage
{
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    PowerCreator.LMS.Web.DL.Project.TOCourseList objCourselist = new PowerCreator.LMS.Web.DL.Project.TOCourseList();
    PowerCreator.LMS.ViewQuery.ViewQuery ov = new PowerCreator.LMS.ViewQuery.ViewQuery();
    PowerCreator.LMS.Web.CMS.New opNews = new PowerCreator.LMS.Web.CMS.New();
    
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "UserLogin.aspx";
    protected string classID = "0";
    protected string courseListID = "0";

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();
        if (Request.QueryString["CourseListID"] != null)
        {
            courseListID = Request.QueryString["CourseListID"].ToString();
            ViewState["CourseListID"] = courseListID;
            classID = Request.QueryString["ClassID"].ToString();
            ViewState["ClassID"] = classID;
            GetThisProject(int.Parse(courseListID));
        }
        BinderRPT();
    }


    private string GetNewSearch()
    {
        string sNewIDList = "0";
        string search = "";
        string rtsearch = "";

        DataTable dt2 = new DataTable();
        string sql = "";

        sql = "select NewID from TNew where  FKNewTrainclaIDList like '%," + classID + ",%'";
        dt2 = ov.Query(sql);
        for (int k = 0; k < dt2.Rows.Count; k++)
        {
            sNewIDList += "," + dt2.Rows[k]["NewID"].ToString();
        }
        dt2.Clear();
        rtsearch = " NewState=1 and NewID in (" + sNewIDList + ")";
        return rtsearch;
    }

    private void BinderRPT()
    {
        DataTable dt = new DataTable();
        string orderby = "NewID desc";
        string search = GetNewSearch();
        int iPageSize = 3;
        dt = opNews.Query(search, orderby, iPageSize, 1);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["NewUrl"] == null)
                dt.Rows[i]["NewUrl"] = "OpenNews.aspx?NewsID=" + dt.Rows[i]["NewID"].ToString();
            else if (dt.Rows[i]["NewUrl"].ToString().Length == 0)
                dt.Rows[i]["NewUrl"] = "OpenNews.aspx?NewsID=" + dt.Rows[i]["NewID"].ToString();

        }


        rpt_New.DataSource = dt;
        rpt_New.DataBind();
    }

    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }

    }
    private void GetThisProject(int clid)
    {
        PowerCreator.LMS.DL.Project.Entity.VOTOCourseList vo= objCourselist.Load(clid);
        
        string[] strscourseid = vo.CourseID.Split(',');
        string str_courseid = "0";
        if (strscourseid.Length > 0)
        {
            for (int j = 0; j < strscourseid.Length; j++)
            {
                string[] courseid = strscourseid[j].Split('#');
                str_courseid += "," + courseid[0].ToString();
            }
        }
        BinderRPTProjectResource(str_courseid);

        lbl_ProjectName2.Text = vo.CourseListName;
    }
    private void BinderRPTProjectResource(string courselist)
    {
        string[] singleCourse = courselist.Split(',');
        string courseIDList_bixiu = string.Empty;
        string courseIDList_xuanxiu = string.Empty;
        string str_courseIDList_wancheng = "0";

        string str_search_wanc = string.Format("select Course_Id from (select Course_Id,isnull(CoursePeriod,0)*3600 as CoursePeriod,sum(SscrlogTotalTime)as SscrlogTotalTime from TO_Course,TStudentStudyCouReslog where Course_Id=FKSscrlogCourseID and FKSscrlogUserID={0} and FKSscrlogTrainclaID={1} group by Course_Id,isnull(CoursePeriod,0)*3600 )as a where SscrlogTotalTime>=CoursePeriod", login.UserID.ToString(), ViewState["ClassID"].ToString());
        DataTable dt_wancID = ov.Query(str_search_wanc);
        if (dt_wancID.Rows.Count > 0)
        {
            for (int j = 0; j < dt_wancID.Rows.Count; j++)
            {
                str_courseIDList_wancheng += "," + dt_wancID.Rows[j]["Course_Id"].ToString().Trim();
            }
        }

        if (courselist.Length > 0)
            courseIDList_bixiu = courselist;
        else
            courseIDList_bixiu = "0";
        
        if (courseIDList_bixiu.Length == 0)
            courseIDList_bixiu = "0";
        string search = string.Empty;
        search = " Used=1 and Course_Id in (" + courseIDList_bixiu + ") and Course_Id not in(" + str_courseIDList_wancheng + ")";
        DataTable dt_bixiu = GetDt(search, singleCourse);

        string str_search = string.Format("select FKUCCourseID from UserCourse where UCUserID={0} and FKUCClassID={1} and UCState=1 and FKUCCourseID not in(" + str_courseIDList_wancheng + ") ", login.UserID.ToString(), ViewState["ClassID"].ToString());
        DataTable dt_xxcourse = ov.Query(str_search);
        courseIDList_xuanxiu = "0";
        if (dt_xxcourse.Rows.Count > 0)
        {
            for (int i = 0; i < dt_xxcourse.Rows.Count; i++)
            {
                courseIDList_xuanxiu += "," + dt_xxcourse.Rows[i]["FKUCCourseID"].ToString().Trim();
            }
        }

        string[] xuanxiuCourse = courseIDList_xuanxiu.Split(',');
        search = " Used=1 and Course_Id in (" + courseIDList_xuanxiu + ")";
        DataTable dt_xuanxiu = GetDt(search, xuanxiuCourse);


        string[] wanchengCourse = str_courseIDList_wancheng.Split(',');
        search = " Used=1 and Course_Id in (" + str_courseIDList_wancheng + ")";
        DataTable dt_wancheng = GetDt(search, wanchengCourse);


        rpt_ProjectResource_Bixiu.DataSource = dt_bixiu;
        rpt_ProjectResource_Bixiu.DataBind();
        rpt_ProjectResource_Xuanxiu.DataSource = dt_xuanxiu;
        rpt_ProjectResource_Xuanxiu.DataBind();
        rpt_wangcheng.DataSource = dt_wancheng;
        rpt_wangcheng.DataBind();

        if (dt_xuanxiu.Rows.Count == 0)
            a_sel.Visible = true;
        else
            a_sel.Visible = false;
    }

    private DataTable GetDt(string search, string[] singleCourse)
    {
        VOTOSchoolStage vostage = new VOTOSchoolStage();
        VOTCatalog vocatalog = new VOTCatalog();
        TOSchoolStage objstage = new TOSchoolStage();
        Catalog objcatalog = new Catalog();
        PowerCreator.LMS.ViewQuery.ViewQuery vo = new PowerCreator.LMS.ViewQuery.ViewQuery();
        PowerCreator.LMS.Web.Course.Course obj = new Course();
        string sql = string.Empty;
        float per = 0;
        float picper = 0;
        float OtherPer = 100;
        DataTable dt01 = new DataTable();
        DataTable dt = new DataTable();
        dt = obj.Query(search);
        dt.Columns.Add("ResourceImageUrl");
        dt.Columns.Add("strxueke");
        dt.Columns.Add("strxueduan");
        dt.Columns.Add("per");
        dt.Columns.Add("str_per");
        dt.Columns.Add("OtherPer");
        float a = 0;
        float b = 100;
        
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            a = 0;
            b = 100;
            per = 0;
            OtherPer = 100;
            picper = 0;
            dt.Rows[i]["ResourceImageUrl"] = "<a href=\"OpenCourse.aspx?CourseID=" + dt.Rows[i]["Course_Id"] + "&ClassID=" + ViewState["ClassID"] + "\" title=\"" + dt.Rows[i]["CourseName"] + "\">立即学习</a>";
            vostage = objstage.Load(int.Parse(dt.Rows[i]["Xueduan"].ToString()));
            dt.Rows[i]["strxueduan"] = vostage.SchoolStage;
            vocatalog = objcatalog.Load(int.Parse(dt.Rows[i]["Xueke"].ToString()));
            dt.Rows[i]["strxueke"] = vocatalog.CatalogName;
            if (login != null)
            {
                //从学习记录表中得到学习时间
                sql = "Select sum(SscrlogTotalTime) as SscrlogTotalTime from TStudentStudyCouReslog where FKSscrlogUserID=" + login.UserID.ToString() + " and FKSscrlogCourseID=" + dt.Rows[i]["Course_Id"].ToString() + " and FKSscrlogTrainclaID=" + ViewState["ClassID"].ToString();
                dt01 = vo.Query(sql);

                if (dt01.Rows.Count == 1)
                {
                    if (singleCourse.Length > 0)
                    {
                        for (int j = 0; j < singleCourse.Length; j++)
                        {
                            if (dt.Rows[i]["Course_Id"].ToString() == singleCourse[j])
                                {
                                    if (dt01.Rows[0]["SscrlogTotalTime"].ToString().Length > 0)
                                    {
                                        a = float.Parse(dt01.Rows[0]["SscrlogTotalTime"].ToString());//学习总时间
                                        b = float.Parse(dt.Rows[i]["CoursePeriod"].ToString()) * 3600;//课程总时间
                                        picper = a / b;
                                        if (picper > 1)
                                            picper = 1;
                                        OtherPer = 100 - picper;
                                    }
                                }
                        }
                    }
                }
            }
            //计算课程完成百分比
            per = picper * 100;
            dt.Rows[i]["per"] = per.ToString();
            if (picper == 100)
                dt.Rows[i]["str_per"] = 1.ToString("p");
            else
                dt.Rows[i]["str_per"] = picper.ToString("p");
            dt.Rows[i]["OtherPer"] = OtherPer;
            
        }
        return dt;
    }

    private string GetPicName(string fileName,int Resourceclass)
    {
        string rt = "images/Exam.jpg";
        if (Resourceclass == 1)
        {
            rt = "000.jpg";
        }
        else if(Resourceclass == 2)
        {
            string[] fileNames = fileName.Split('.');
            if (fileNames.Length == 2)
                rt = fileNames[0].ToString() + ".jpg";
        }
        return rt;
    }
    private string GetPath(string Path, int Resourceclass)
    {
        string rt = Path;
        if (Resourceclass != 1 && Resourceclass != 2)
        {
            rt = "";
        }
        return rt;
    }

    protected void rpt_ProjectResource_Xuanxiu_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        LinkButton lnkdel = (LinkButton)e.Item.FindControl("lnkbtn_del");
        if (e.CommandName.ToLower() == "lsdel")
        {
            PowerCreator.LMS.Web.Course.UserCourse objusercourse=new PowerCreator.LMS.Web.Course.UserCourse();
            PowerCreator.LMS.Course.Entity.UserCourse ovusercourse = new PowerCreator.LMS.Course.Entity.UserCourse();
            string str_search = string.Format("select UCID from UserCourse where UCUserID={0} and FKUCClassID={1} and FKUCCourseID={2} and UCState=1", login.UserID.ToString(), ViewState["ClassID"].ToString(), e.CommandArgument.ToString());
            DataTable dt_course = ov.Query(str_search);
            if (dt_course.Rows.Count > 0)
            {
                int courseid = int.Parse(dt_course.Rows[0]["UCID"].ToString());
                if (objusercourse.RemoveUserCourse(courseid))
                {
                    GetThisProject(int.Parse(ViewState["CourseListID"].ToString()));
                    BinderRPT();
                }
            }
            else
            {
                Script.Alert(this, "", "该记录不正确,请联系管理员！");
                return;
            }
            
        }
    }

    protected void rpt_ProjectResource_Xuanxiu_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
                
                lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
                
            }
        }
    }
}